﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Model;
using DataAccess;

namespace Project
{
    /// <summary>
    /// Interaction logic for AllComplaints.xaml
    /// </summary>
    public partial class AllComplaints : UserControl
    {
        public List<ShowablePerson> Persons { get; set; }
        public string VictimId { get; set; }
        public string AggressorId { get; set; }
        private List<ComplaintInformation> complaints; 
        
        public AllComplaints()
        {
            InitializeComponent();
            Persons = PersonAndActorDataAccess.GetAllShowablePersons();
            tbVictimName.ItemsSource = Persons;
            tbVictimCi.ItemsSource = Persons;
            tbAggressorName.ItemsSource = Persons;
            tbAggressorCi.ItemsSource = Persons;
            complaints = ComplaintDataAccess.GetAllComplaintsInformation();
            listViewComplaints.DataContext = complaints;
            tbCaseNumber.ItemsSource = complaints;
            listViewComplaints.ContextMenuOpening += FilterMenu;
            UIWindowManager.SetWindowTitle(this,"Brigada De Protección a la Familia - Ver Denuncias");
            HideMenuOptionsDependingOfUser();
        }

        private void HideMenuOptionsDependingOfUser()
        {
            if (UserControllerAndDataAccess.IsUserLogged())
            {
                if (UserControllerAndDataAccess.IsDirectorLogged)
                {
                    ModComplaintOp.Visibility = Visibility.Collapsed;
                    ModifyMedicalOp.Visibility = Visibility.Collapsed;
                    ModifyDirectActionOp.Visibility = Visibility.Collapsed;
                }
                else
                {
                    if (UserControllerAndDataAccess.IsPsicologistLogged)
                    {
                        AudiencesOp.Visibility = Visibility.Collapsed;
                        MedicalOps.Visibility = Visibility.Collapsed;
                        ComplaintOps.Visibility = Visibility.Collapsed;
                        CompleteComplaintRegistration.Visibility = Visibility.Collapsed;
                        PhotoAlbumOp.Visibility = Visibility.Collapsed;
                        ActionOps.Visibility = Visibility.Collapsed;
                        Citations.Visibility = Visibility.Collapsed;
                        ViewDeclarationsOp.Visibility = Visibility.Collapsed;
                    }
                    else
                        RemitCase.Visibility = Visibility.Collapsed;
                }
            }
        }


        private void FilterResults()
        {
            if (complaints != null)
            {
                listViewComplaints.DataContext = ComplaintDataAccess.GetComplaintsBy(complaints, VictimId, AggressorId, null, null,null, tbCaseNumber.Text);
            }
        }

        private void ViewComplaint_Click(object sender, RoutedEventArgs e)
        {
            
            if ((listViewComplaints.SelectedValue as ComplaintInformation) != null)
            {
                Window w = Window.GetWindow(this.Parent);
                ((MainWindow)w).Content.Children.Clear();
                long idComplaint = (listViewComplaints.SelectedValue as ComplaintInformation).NumberOfCase;
                ((MainWindow)w).Content.Children.Add(new ViewComplaint(idComplaint));
            }
        }


        private void EditCopmlaint_Click(object sender, RoutedEventArgs e)
        {
           
            if ((listViewComplaints.SelectedValue as ComplaintInformation) != null)
            {
                Window w = Window.GetWindow(this.Parent);
                ((MainWindow)w).Content.Children.Clear();
                long idComplaint = (listViewComplaints.SelectedValue as ComplaintInformation).NumberOfCase;
                var editCompl = new ModifyComplaint(idComplaint);
                ((MainWindow)w).Content.Children.Add(editCompl);
            }
        }

        private void ViewPsico_Click(object sender, RoutedEventArgs e)
        {
            //Content.Children.Clear();
            //Content.Children.Add(new AllPsicologistReports());
        }

        private void MenuItem_Click_1(object sender, RoutedEventArgs e)
        {
            
            if ((listViewComplaints.SelectedValue as ComplaintInformation) != null)
            {
                Window w = Window.GetWindow(this.Parent);
                ((MainWindow)w).Content.Children.Clear();
                long idComplaint = (listViewComplaints.SelectedValue as ComplaintInformation).NumberOfCase;
                ((MainWindow)w).Content.Children.Add(new ViewRequestingMedical(idComplaint));
            }
        }

        private void MenuItem_Click_2(object sender, RoutedEventArgs e)
        {
           
            if ((listViewComplaints.SelectedValue as ComplaintInformation) != null)
            {
                Window w = Window.GetWindow(this.Parent);
                ((MainWindow)w).Content.Children.Clear();
                long idComplaint = (listViewComplaints.SelectedValue as ComplaintInformation).NumberOfCase;
                ((MainWindow)w).Content.Children.Add(new UpdateRequestingMedical(idComplaint));
            }
        }

        private void MenuItem_Click_3(object sender, RoutedEventArgs e)
        {
            if ((listViewComplaints.SelectedValue as ComplaintInformation) != null)
            {
                Window w = Window.GetWindow(this.Parent);
                ((MainWindow)w).Content.Children.Clear();
                long idComplaint = (listViewComplaints.SelectedValue as ComplaintInformation).NumberOfCase;
                ((MainWindow)w).Content.Children.Add(new ViewCitationsContent(idComplaint));
            }
        }

        private void ViewAudiences_Click(object sender, RoutedEventArgs e)
        {
            if ((listViewComplaints.SelectedValue as ComplaintInformation) != null)
            {
                Window w = Window.GetWindow(this.Parent);
                ((MainWindow)w).Content.Children.Clear();
                long idComplaint = (listViewComplaints.SelectedValue as ComplaintInformation).NumberOfCase;
                ((MainWindow)w).Content.Children.Add(new AudiencesOfComplaint(idComplaint));
            }
        }

        private void ViewAllPsicologist_Click(object sender, RoutedEventArgs e)
        {
            if ((listViewComplaints.SelectedValue as ComplaintInformation) != null)
            {
                Window w = Window.GetWindow(this.Parent);
                ((MainWindow)w).Content.Children.Clear();
                long idComplaint = (listViewComplaints.SelectedValue as ComplaintInformation).NumberOfCase;
                ((MainWindow)w).Content.Children.Add(new AllPsicologistReports(idComplaint));
            }
        }

        private void MenuItem_Click_5(object sender, RoutedEventArgs e)
        {
            if ((listViewComplaints.SelectedValue as ComplaintInformation) != null)
            {
                Window w = Window.GetWindow(this.Parent);
                ((MainWindow)w).Content.Children.Clear();
                long idComplaint = (listViewComplaints.SelectedValue as ComplaintInformation).NumberOfCase;
                ((MainWindow)w).Content.Children.Add(new RequestingMedical(idComplaint));
            }
        }

        

        private void PhotoAlbumCopmlaint_Click(object sender, RoutedEventArgs e)
        {
            if ((listViewComplaints.SelectedValue as ComplaintInformation) != null)
            {
                Window w = Window.GetWindow(this.Parent);
                ((MainWindow)w).Content.Children.Clear();
                long idComplaint = (listViewComplaints.SelectedValue as ComplaintInformation).NumberOfCase;
                string compRegDate = (listViewComplaints.SelectedValue as ComplaintInformation).DateOfCase;
                ((MainWindow)w).Content.Children.Add(new ComplaintPhotos(idComplaint, compRegDate));
            }
        }

        private void NameDropDownClosing(object sender, RoutedPropertyChangingEventArgs<bool> e)
        {
            if (rbtnByName.IsChecked == true)
            {
                ShowablePerson selectedPerson = tbVictimName.SelectedItem as ShowablePerson;
                if (selectedPerson != null && tbVictimName.IsEnabled)
                {
                    tbVictimCi.Text = selectedPerson.CiPassport.ToString();
                    VictimId = selectedPerson.Id;
                    FilterResults();
                }
                ShowablePerson selectedPerson2 = tbAggressorName.SelectedItem as ShowablePerson;
                if (selectedPerson2 != null && tbAggressorName.IsEnabled)
                {
                    tbAggressorCi.Text = selectedPerson2.CiPassport.ToString();
                    AggressorId = selectedPerson2.Id;
                    FilterResults();
                }
            }
        }

        private void CiDropDownClosing(object sender, RoutedPropertyChangingEventArgs<bool> e)
        {
            if (rbtnByCi.IsChecked == true)
            {
                ShowablePerson selectedPerson = tbVictimCi.SelectedItem as ShowablePerson;
                if (selectedPerson != null && tbVictimCi.IsEnabled)
                {
                    tbVictimName.Text = selectedPerson.FullName;
                    VictimId = selectedPerson.Id;
                    FilterResults();
                }
                ShowablePerson selectedPerson2 = tbAggressorCi.SelectedItem as ShowablePerson;
                if (selectedPerson2 != null && tbAggressorCi.IsEnabled)
                {
                    tbAggressorName.Text = selectedPerson2.FullName;
                    AggressorId = selectedPerson2.Id;
                    FilterResults();
                }
            }
        }

        private void rbtnByName_Checked(object sender, RoutedEventArgs e)
        {
            tbVictimName.IsEnabled = true;
            tbAggressorName.IsEnabled = true;
            tbAggressorCi.IsEnabled = false;
            tbVictimCi.IsEnabled = false;
        }

        private void rbtnByCi_Checked(object sender, RoutedEventArgs e)
        {
            tbVictimName.IsEnabled = false;
            tbAggressorName.IsEnabled = false;
            tbAggressorCi.IsEnabled = true;
            tbVictimCi.IsEnabled = true;
        }

        private void VerifyPreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (Regex.IsMatch(e.Text.ToString(), "^[A-Za-z´áéíóúAÉÍÓÚÑñ ]+$") && (sender as AutoCompleteBox).Text.Length < 50)
                e.Handled = false;
            else
                e.Handled = true;
        }
        private void VerifyPreviewNumberTextInput(object sender, TextCompositionEventArgs e)
        {
            if (Regex.IsMatch(e.Text.ToString(), "^[0-9]+$") && (sender as AutoCompleteBox).Text.Length < 10)
                e.Handled = false;
            else
                e.Handled = true;
        }

        private void tbCaseNumber_DropDownClosing(object sender, RoutedPropertyChangingEventArgs<bool> e)
        {
            ComplaintInformation comp = tbCaseNumber.SelectedItem as ComplaintInformation;
            if (comp != null)
            {
                FilterResults();
            }
        }

        private void TbTextChanged(object sender, RoutedEventArgs e)
        {
            var tbox = (sender as AutoCompleteBox);
            if (String.IsNullOrWhiteSpace(tbox.Text))
            {
                if (tbox == tbVictimName)
                {
                    tbVictimCi.Text = "";
                    VictimId = null;
                }
                if (tbox == tbVictimCi)
                {
                    tbVictimName.Text = "";
                    VictimId = null;
                }
                if (tbox == tbAggressorName)
                {
                    tbAggressorCi.Text = "";
                    AggressorId = null;
                }
                if (tbox == tbAggressorCi)
                {
                    tbAggressorName.Text = "";
                    AggressorId = null;
                }
                FilterResults();
            }
        }

        private void MenuItem_OnClick(object sender, RoutedEventArgs e)
        {
            if ((listViewComplaints.SelectedValue as ComplaintInformation) != null)
            {
                Window w = Window.GetWindow(this.Parent);
                ((MainWindow) w).Content.Children.Clear();
                var complaintInformation = listViewComplaints.SelectedValue as ComplaintInformation;
                long idComplaint = complaintInformation.NumberOfCase;
                ((MainWindow) w).Content.Children.Add(new ReportOfConclusion(idComplaint));
            }
        }

        private void RemissionOfCase_Click(object sender, RoutedEventArgs e)
        {
            if ((listViewComplaints.SelectedValue as ComplaintInformation) != null)
            {
                Window w = Window.GetWindow(this.Parent);
                ((MainWindow)w).Content.Children.Clear();
                var complaintInformation = listViewComplaints.SelectedValue as ComplaintInformation;
                long idComplaint = complaintInformation.NumberOfCase;
                ((MainWindow)w).Content.Children.Add(new RemissionOfCaseUI(idComplaint));
            }
        }

        private void MenuItem_Click_4(object sender, RoutedEventArgs e)
        {
            if ((listViewComplaints.SelectedValue as ComplaintInformation) != null)
            {
                Window w = Window.GetWindow(this.Parent);
                ((MainWindow)w).Content.Children.Clear();
                long idComplaint = (listViewComplaints.SelectedValue as ComplaintInformation).NumberOfCase;
                ((MainWindow)w).Content.Children.Add(new ViewDirectAction(idComplaint));
            }
        }

        private void MenuItem_Click_6(object sender, RoutedEventArgs e)
        {
            if ((listViewComplaints.SelectedValue as ComplaintInformation) != null)
            {
                Window w = Window.GetWindow(this.Parent);
                ((MainWindow)w).Content.Children.Clear();
                long idComplaint = (listViewComplaints.SelectedValue as ComplaintInformation).NumberOfCase;
                ((MainWindow)w).Content.Children.Add(new UpdateDirectAction(idComplaint));
            }
        }

        private void ViewDeclarationsOp_OnClick(object sender, RoutedEventArgs e)
        {
            if ((listViewComplaints.SelectedValue as ComplaintInformation) != null)
            {
                Window w = Window.GetWindow(this.Parent);
                ((MainWindow)w).Content.Children.Clear();
                long idComplaint = (listViewComplaints.SelectedValue as ComplaintInformation).NumberOfCase;
                ((MainWindow)w).Content.Children.Add(new ViewDeclarations(idComplaint));
            }
        }

        private void FilterMenu(object sender, ContextMenuEventArgs e)
        {
            if ((listViewComplaints.SelectedValue as ComplaintInformation) != null)
            {
                if ((listViewComplaints.SelectedValue as ComplaintInformation).VictimCi == "0")
                {
                    string message = "Termine de registrar la denuncia para habilitar las opciones";
                    ViewComplaintOp.IsEnabled = false;
                    ViewComplaintOp.ToolTip = message;
                    ModComplaintOp.IsEnabled = false;
                    ModComplaintOp.ToolTip = message;
                    RemitCase.IsEnabled = false;
                    RemitCase.ToolTip = message;
                    GenerateConclusion.IsEnabled = false;
                    GenerateConclusion.ToolTip = message;
                    PhotoAlbumOp.IsEnabled = false;
                    PhotoAlbumOp.ToolTip = message;
                    ViewDeclarationsOp.IsEnabled = false;
                    ViewDeclarationsOp.ToolTip = message;
                    var tooltip = new TextBlock();
                    tooltip.Text = "Debe terminar de registrar la denuncia de esta acción directa";
                    tooltip.Foreground = Brushes.Red;
                    tooltip.FontSize = 12;
                    tooltip.FontWeight = FontWeights.Bold;
                    CompleteComplaintRegistration.ToolTip = tooltip;
                    CompleteComplaintRegistration.Visibility = Visibility.Visible;

                }
                else
                {
                    ViewComplaintOp.IsEnabled = true;
                    ViewComplaintOp.ToolTip = null;
                    ModComplaintOp.IsEnabled = true;
                    ModComplaintOp.ToolTip = null;
                    RemitCase.IsEnabled = true;
                    RemitCase.ToolTip = null;
                    GenerateConclusion.IsEnabled = true;
                    GenerateConclusion.ToolTip = null;
                    PhotoAlbumOp.IsEnabled = true;
                    PhotoAlbumOp.ToolTip = null;
                    ViewDeclarationsOp.IsEnabled = true;
                    ViewDeclarationsOp.ToolTip = null;
                    CompleteComplaintRegistration.Visibility = Visibility.Collapsed;
                }
            }
        }

        private void CompleteComplaintRegistration_OnClick(object sender, RoutedEventArgs e)
        {
            if ((listViewComplaints.SelectedValue as ComplaintInformation) != null)
            {
                Window w = Window.GetWindow(this.Parent);
                ((MainWindow)w).Content.Children.Clear();
                long idComplaint = (listViewComplaints.SelectedValue as ComplaintInformation).NumberOfCase;
                var completeComplaint = new RegisterComplaint(idComplaint);
                ((MainWindow)w).Content.Children.Add(completeComplaint);
            }
        }
    }
}
